package cn.tedu.order.service;

import cn.tedu.order.entity.Order;
import cn.tedu.order.feign.AccountClient;
import cn.tedu.order.feign.EasiIdCliect;
import cn.tedu.order.feign.StorageClient;
import cn.tedu.order.mapper.OrderMapper;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class OrdereServiceImpl implements OrderService{
    @Autowired
    private EasiIdCliect easiIdCliect ;
    @Autowired
    private AccountClient accountClient ;
    @Autowired
    private StorageClient storageClient ;
    //先临时随机产生id，完成发号器后删除这段代码
//    Long orderId = Math.abs(new Random().nextLong());//Math.abs 生成一个绝对值保证不为负数
    @Autowired
    private OrderMapper orderMapper ;
    @Override
    public void create(Order order) {
        String s = easiIdCliect.getId("order_business");
        Long orderId = Long.valueOf(s);
        order.setId(orderId);
        orderMapper.create(order);

        //远程调用库存，减少库存
        storageClient.decrease(order.getProductId(),order.getCount());
        accountClient.decrease(order.getUserId(),order.getMoney());
    }
}
